const memberApi = require('../../api/member');

Page({
  data: {
    oldPassword: '',
    newPassword: '',
    confirmPassword: ''
  },
  onInputOld(e) {
    this.setData({ oldPassword: e.detail });
  },
  onInputNew(e) {
    this.setData({ newPassword: e.detail });
  },
  onInputConfirm(e) {
    this.setData({ confirmPassword: e.detail });
  },
  onSave() {
    if (!this.data.oldPassword || !this.data.newPassword || !this.data.confirmPassword) {
      wx.showToast({ title: '请填写完整', icon: 'none' });
      return;
    }
    if (this.data.newPassword !== this.data.confirmPassword) {
      wx.showToast({ title: '两次密码不一致', icon: 'none' });
      return;
    }
    memberApi.updatePassword({
      oldPassword: this.data.oldPassword,
      newPassword: this.data.newPassword
    }).then(res => {
      if (res.code === 0) {
        wx.showToast({ title: '修改成功', icon: 'success' });
        setTimeout(() => {
          wx.navigateBack();
        }, 1000);
      } else {
        wx.showToast({ title: res.msg || '修改失败', icon: 'none' });
      }
    }).catch(() => {
      wx.showToast({ title: '请求失败', icon: 'none' });
    });
  }
}); 